package com.koushikdutta.ion.sample;

import com.example.ohosasynclibrary.async.util.LogUtil;
import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.LogUtils;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;

import static org.junit.Assert.assertEquals;

public class ExampleOhosTest {
    @Test
    public void testBundleName() {
        final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
        assertEquals("com.koushikdutta.ion.sample", actualBundleName);
    }

    @Test
    public void asJsonObject() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        Ion.with(context)
                .load("https://httpbin.org/get")
                .asJsonObject()
                .setCallback((e, result) -> {
                    LogUtil.error("asJson", result.toString());
                    Assert.assertTrue(result.toString().length() > 0);
                });
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void downloadFile(){
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        Ion.with(context)
                .load("https://httpbin.org/image/jpeg")
                // can also use a custom callback
                .progress((downloaded, total) -> {
                })
                .write(new File(context.getCacheDir(), "file-test.jpeg"))
                .setCallback((e, file) -> {
                    // download done...
                    // do stuff with the File or error
                    try {
                        LogUtil.error("File Path", file.getCanonicalPath());
                        Assert.assertTrue(file.getTotalSpace() > 0);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                });

        try {
            Thread.sleep(7000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}